/**
 * created by lht 2008-9-18
 */

package com.hollycrm.ahunicom.sheet.maintenance.logic;

import java.util.Iterator;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.SessionFactoryUtils;
import com.hollycrm.ahunicom.sheet.ExtendDAO;
import com.hollycrm.ahunicom.sheet.maintenance.hibernate.TblSheetEventParameter;
import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.usersupport.User;

public class EventParameterLogic extends ExtendDAO{
	
    private Session ss=null;

    public Object update(Object ob, String str) {
    	
		HibernateTemplate ht = getHibernateTemplate();
		SessionFactory sessionFactory = ht.getSessionFactory();
//         Query q = ss.createQuery("select count(a) from TblSheetEventParameter a ");
		this.FormToDTOExchange(ob, this.getPojo());
		
		try {		
			ss = sessionFactory.openSession();
			ss.update((TblSheetEventParameter)this.getPojo(), str);
			ss.flush();
			return new Integer(1);
			
		}catch (Exception e) {			
			//throw SessionFactoryUtils.convertHibernateAccessException(e);
			e.printStackTrace();
			return new Integer(0);
		}finally{
      		if(ss!=null)
      		{
      			try {
  					ss.close();
  				} catch (HibernateException e) {
  					e.printStackTrace();
  				}
      		}
       }

	}
    
    public Object load(Object ob, String str) {
    	
		HibernateTemplate ht = getHibernateTemplate();
		SessionFactory sessionFactory = ht.getSessionFactory();
//         Query q = ss.createQuery("select count(a) from TblSheetEventParameter a ");
		
		try {		
			ss = sessionFactory.openSession();
			Object obj =ss.load(this.getPojo().getClass(), str);
			ss.flush();
			return obj;
			
		}catch (HibernateException e) {			
			//throw SessionFactoryUtils.convertHibernateAccessException(e);
			e.printStackTrace();
		}finally{
      		if(ss!=null){
      			try {
  					ss.close();
  				} catch (HibernateException e) {
  					e.printStackTrace();
  				}
      		}
       }
		return null;
    }

	public Object save(Object ob) {
		
			HibernateTemplate ht = getHibernateTemplate();
			this.FormToDTOExchange(ob, this.getPojo());
			try {		
				//ss = sessionFactory.openSession();
				//java.io.Serializable id = ss.save((TblSheetEventParameter)this.getPojo());
				//ss.flush();
				java.io.Serializable id = ht.save((TblSheetEventParameter)this.getPojo());
				if(id != null){
					return id.toString();
				}else{
					return null;
				}
				
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}finally{
//	      		if(ss!=null){
//	      			try {
//	  					ss.close();
//	  					
//	  				} catch (HibernateException e) {
//	  					e.printStackTrace();
//	  				}
//	      		}
	       }
	}
	
	
    public Object elseQuery(Object ob,String str) {
	       HibernateTemplate ht = getHibernateTemplate();
	       SessionFactory sessionFactory = ht.getSessionFactory();

	     try {
	       ss = sessionFactory.openSession();
	       String problemTypeId = (String)ob;
	       Query q = ss.createQuery("select u from u in " + TblSheetEventParameter.class + " where u.problemTypeId='" + problemTypeId.toString() + "'" + " and u.placeId='" + str.toString() + "'");
	       Iterator iterate = q.iterate();
	       
	       if(iterate != null && iterate.hasNext()){
	    	   return Integer.valueOf("1");
	       }
	       else{
		       return Integer.valueOf("0");
	       }

	     } catch (HibernateException ex) {
	        ex.printStackTrace();
	     }finally{
	    		if(ss!=null){
	    			try {
						ss.close();
					} catch (HibernateException e) {
						e.printStackTrace();
					}
	    		}
	     }
	     return Integer.valueOf("0");
 }
 
}
